<template>
          <div class="boxsty">
            <div class="flex justifyContentSpaceBetween">
              <div class="tabletext">测评发起( 表3-5 )</div>
              <div class="tablelink">
                <el-link icon="el-icon-plus" id="newAdd"  v-if="permissions.ceping_cepinghdglb_add" type="primary" @click="addOrUpdateActiviy($event,null)" :underline="false">新增测评会议</el-link>
              </div>
            </div>
            <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList_new()" class="form-inline">
              <el-form-item label="测评会议名称">
                <el-input v-model="dataForm.hdName" style="width:300px" placeholder="请输入测评会议名称"></el-input>
              </el-form-item>
              <el-form-item label="测评会议状态">
                <el-select v-model="dataForm.hdZt" placeholder="请选择会议状态" :clearable="clearable" class="xiaLa">
                  <el-option label="未开始" value="0"></el-option>
                  <el-option label="进行中" value="1"></el-option>
                  <el-option label="已结束" value="2"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="关联类型">
                <el-select v-model="dataForm.hdCppfs" placeholder="请选择关联类型" :clearable="clearable" class="xiaLa">
                  <el-option label="单票" value="1"></el-option>
                  <el-option label="部门" value="2"></el-option>
                  <el-option label="个人" value="3"></el-option>
                  <el-option label="部门与个人" value="4"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-search" type="primary" class="marginLeft" @click="getDataList_new()">检索</el-button>
              </el-form-item>
            </el-form>

            <div class="avue-crud" style="height:60vh;">
              <el-collapse v-model="activeName_35" @change="selinformation_new(index)" accordion v-for="(record, index) in dataList_35" :key="index">
                <el-collapse-item :name=index>
                  <template slot="title">
                    <div class="collapseBox">
                      <div class="flex alignItemsCenter">
                        <span class="numTip">{{ selxh(index) }}</span>
                        <span class="titleBox">{{ record.hdName }} (<span :class="hdZt(determine(record.hdZt, record.hdJssj))">{{ determine(record.hdZt, record.hdJssj) }}</span>)</span>
                      </div>
                      <div class="collapseSummary flex justifyContentSpaceBetween">
                        <div>
                          <div class="mb5"><span>会议开始时间：{{ record.hdKssj }}</span><span class="marginLeft">会议结束时间：{{ record.hdJssj }}</span></div>
                          <div class="mb5"><span>关联类型：{{ convertZt(record.hdCppfs) }}</span><span class="marginLeft">创建时间：{{ $moment(record.createTime).format("YYYY-MM-DD") }}</span></div>
                          <div><span>创建人：{{ record.createUserName }}</span><span class="marginLeft">审核状态：<span :class="Zt(ConvertShZt( record.hdSh))">{{ ConvertShZt( record.hdSh) }}</span></span><span class="marginLeft">是否联合出票：{{ record.hdSflhcp == 1 ? "是" : "否" }}</span></div>
                        </div>

                        <div>
                          <el-button v-if="permissions.ceping_cepinghdglb_add && record.hdSh == '0'" type="primary" plain @click="addOrUpdateActSheets( $event,record,null,index)">添加测评票</el-button>
                          <el-button v-if="permissions.ceping_cepinghdglb_view && (record.hdZt == '1' || record.hdZt == '2')" type="primary" plain @click="hdpgkOpen($event,record)">会议概况</el-button>
                          <el-button v-if="permissions.ceping_cepinghdglb_view && record.hdZt == '2'" type="primary" plain @click="hdhzOpen($event,record)">会议汇总</el-button>
                          <!-- <el-button v-if="permissions.ceping_cepinghdglb_view" type="primary" plain @click="szbgOpen($event,record.id,record.hdName)">查看述职报告</el-button> -->
                          <el-button v-if="permissions.ceping_cepinghdglb_edit && record.hdSh == '0'" type="primary" plain @click="addOrUpdateActiviy($event,record.id)">编辑</el-button>
                          <el-button v-if="permissions.ceping_cepinghdglb_edit && record.hdSh == '0'" type="primary" plain @click="toExamine_new($event,record)">提交审核</el-button>
                          <el-button v-if="permissions.ceping_cepinghdglb_edit && record.hdSh == '0'" type="danger" plain @click="deleteActivity($event,record.id)">删除</el-button>
                        </div>
                      </div>
                    </div>
                  </template>

                  <div v-for="(data, index) in record.sjlist" :key="index">
                    <div class="custom-tree-node flex justifyContentSpaceBetween">
                      <div class="flex alignItemsCenter">
                        <span class="numTip">{{ index + 1 }}</span>
                        <span>{{ data.sheetName }}</span>
                        <!-- <span>({{ data.cppQz }}%)</span> -->
                        <span>{{ data.isUnion == '1' ? "（联合主票）" : "" }}</span>
                      </div>
                      <div class="hangJu">
                        <el-button v-if="permissions.ceping_cepinghdglb_view  && (record.hdZt == '1' || record.hdZt == '2')" type="primary" plain @click="cppgkOpen($event,record,data)">测评票概况</el-button>
                        <el-button v-if="permissions.ceping_cepinghdglb_view" type="primary" plain @click="reading(record)">测评票预览</el-button>
                        <el-button v-if="permissions.ceping_cepinghdglb_view && record.hdZt == '2'" type="primary" plain @click="cpphzOpen($event,record,data)">测评票汇总</el-button>
                        <el-button v-if="permissions.ceping_cepinghdglb_edit && record.hdSh == '0'" type="primary" plain @click="addOrUpdateActSheets($event,record,data.id,index)">编辑</el-button>
                        <el-button v-if="permissions.ceping_cepinghdglb_edit && record.hdSh == '0'" type="danger" plain @click="deleteActSheet(data.id)">删除</el-button>
                      </div>
                    </div>
                  </div>
                </el-collapse-item>
              </el-collapse>
            </div>

            <div class="avue-crud__pagination">
              <el-pagination @size-change="sizeChangeHandle_new" @current-change="currentChangeHandle_new" :current-page="pageIndex_35" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize_35" :total="totalPage_35" background layout="total, sizes, prev, pager, next, jumper">
              </el-pagination>
            </div>
                    <hdglb v-if="hdglAddOrUpdateVisible" ref="hdglAddOrUpdate" @refreshList="zjOperation"></hdglb>
        <!--新增、编辑测评 表单3-5-->
        <activity v-if="activityVisible" ref="newAcitivy" @refreshList="zjOperation"></activity>
        <!--新增、编辑测评会议测评票-->
        <hdgl-cpp v-if="hdglCppAddOrUpdateVisibale" ref="hdglCppAddOrUpdate" @refreshSubList="getDataList"></hdgl-cpp>
        <activity-sheet v-if="hdglCppAddOrUpdateVisibale_new" ref="activitySheet" @refreshSubList="getDataList_new"></activity-sheet>
        <!--查看述职报告-->
        <hdglbSz v-if="hdglSzAddOrUpdateVisibale" ref="hdglSzAddOrUpdate" @refreshSubList="getDataList"></hdglbSz>
        <!--  -->
        <CepingpiaoYulan v-if="cepingpiaoYulanVisible" ref="cepingpiaoYulan"></CepingpiaoYulan>
        <!-- 测评票概况  -->
        <!-- 测评票预览  -->
        <cepingphz v-if="cepingphzVisible" ref="cepingPhz"></cepingphz>

        <!-- 测评票汇总  -->
        <cepinghdcphzDialog ref="cepinghdcphzDialog"></cepinghdcphzDialog>
        <!-- 会议汇总  -->
        <cepinghdzhhzDialog ref="cepinghdzhhzDialog"></cepinghdzhhzDialog>
       <CepingpiaoYulan3 v-if="cepingpiaoYulanVisible3" ref="cepingpiaoYulan3"></CepingpiaoYulan3>
      <CepingpiaoYulan4 v-if="cepingpiaoYulanVisible4" ref="cepingpiaoYulan4"></CepingpiaoYulan4>
      <CepingpiaoYulan5 v-if="cepingpiaoYulanVisible5" ref="cepingpiaoYulan5"></CepingpiaoYulan5>       
          </div>
</template>

<script>
/*!
 * 绩效考核-测评发起列表
 *
 * @author zmk
 * @date 2021-06-13 12:11:35
 * @update 2021-06-25 09:02 linh  测评会议[增、删、改、查]
 * @update 2021-06-25 18:16 linh  测评票[增、删、改、查]
 */
// import {fetchList, cepingOpen, delObj, updateHdSh} from '@/api/ceping/cepinghdglb'
// import {selhdidList, delObjCpp, selCppList} from '@/api/ceping/cepinghdglcppmxb'

import Hdglb from './cepinghdglb-form'
import hdglCpp from './cepinghdglcppmxb-form'
import hdglbSz from './cepinghdglbsz-form'
import CepingpiaoYulan from './cepingpiaoYulan'
import cepingphz from "./cepingphz-form";
import cepinghdcphzDialog from "./cepinghdcphzDialog";
import cepinghdzhhzDialog from "../cepinghdglsh/cepinghdzhhzDialog";
import {mapGetters} from 'vuex'

import activity from './cepinghdglb-form-3-5'
import activitySheet from './eval-activity-sheet'
import {fetchList_new, cepingOpen_new, delObj_new, updateHdSh_new} from '@/api/ceping/eval-activity'
import {selhdidList_new, delObjCpp_new, selCppList_new} from '@/api/ceping/eval-activity-sheet'
import CepingpiaoYulan3 from '../cepingcpppypzb/cepingpiaoYulan3'
import CepingpiaoYulan4 from '../cepingcpppypzb/cepingpiaoYulan4'
import CepingpiaoYulan5 from '../cepingcpppypzb/cepingpiaoYulan5'

export default {
  data() {
    return {
      dataForm: {
        hdName: '',
        hdZt: '',
        hdCppfs: '',
      },
      dataForm_35: {
        hdName: '',
        hdZt: '',
        hdCppfs: '',
      },
      index: '',
      index_35: '',
      activeName: '',
      activeName_35: '',
      itemData: '',
      clearable: true,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataList_35: [],
      pageIndex_35: 1,
      pageSize_35: 10,
      totalPage_35: 0,     
      dataListLoading: false,
      cepingpiaoYulanVisible: false,
      hdglAddOrUpdateVisible: false,
      activityVisible:false,
      hdglCppAddOrUpdateVisibale: false,
      hdglCppAddOrUpdateVisibale_new: false,
      hdglSzAddOrUpdateVisibale: false,
      cepingphzVisible: false,
      cepingpiaoYulanVisible3:false,
      cepingpiaoYulanVisible4:false,
      cepingpiaoYulanVisible5:false,     

      mxUpdateOrAddIndex: '',
      mxUpdateOrAddPid: '',
      activeTab:'sheet1-2'
      
    }
  },
  components: {
    Hdglb,
    hdglCpp,
    hdglbSz,
    CepingpiaoYulan,
    cepingphz,
    cepinghdcphzDialog,
    cepinghdzhhzDialog,
    activity,
    activitySheet,
    CepingpiaoYulan3,
    CepingpiaoYulan4,
    CepingpiaoYulan5    
  },
  created() {
    // this.getDataList();
    this.getDataList_new();
    // selCppList().then(res => {
    //   this.itemData = res.data.data;
    // });
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    tabClick(tab, event) {
     //console.log("tabClick");
      if(tab.name == 'sheet3-5'){
       //console.log("tabClick sheet3-5");
          this.getDataList_new();
      }
    },
    //测评启动
    cepingOpen(e, id) {
      e.stopPropagation();

      cepingOpen(id).then(data => {
        //缺少返回错误处理
        this.$notify.closeAll();
        this.$notify.success('操作成功');
        this.getDataList();
      }).catch(error => {
       //console.log(error);
      });
    },

        //审核处理
    toExamine_new(e, data) {
      e.stopPropagation();
      var mxblist = [];

      selhdidList_new(data.id).then(response => {
        mxblist = response.data.data;

          if (mxblist.length == 0) {
            this.$notify.closeAll();
            this.$notify.error("没有测评票，无法提交审核。")
            return;
          }

        var bm, gr, lhzp = 0;
        for (var a = 0; a < mxblist.length; a++) {
         //console.log('被测评对象',mxblist[a].cpgzBpfdx);
          if (mxblist[a].cpgzBpfdx == "2") 
                      bm = bm + 1;
          else if (mxblist[a].cpgzBpfdx == "3") {
            gr = gr + 1;
          }
          if (mxblist[a].isUnion == '1') {
            lhzp = lhzp + 1;
          }
        }        

        if (data.hdCppfs == "4") {
          if (bm == 0 || gr == 0) {
            this.$notify.closeAll();
            this.$notify.error("部门与个人类型必须添加一张部门票与一张个人票。");
            return;
          }
        }
        if (data.hdSflhcp == 1 && lhzp == 0) {
          this.$notify.closeAll();
          this.$notify.error("联合出票必须选择一张联合主票。");
          return;
        }
        if (data.hdSflhcp == '1' && lhzp > 1) {
          this.$notify.closeAll();
          this.$notify.error("联合主票只能有一张。");
          return;
        }

        var _this = this;
        this.$confirm('是否确定执行，执行后信息不可修改。', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          data.hdSh = "1";
          updateHdSh_new(data).then(data => {
            //缺少返回错误处理
            _this.$notify.success('操作成功');
            _this.getDataList_new();
          }).catch(error => {
           //console.log(error);
          });
        }).catch(error => {
         //console.log(error);
        });
      });
    },
    getCppName(findId, key) {
      let lResult = "";
      this.itemData.find(value => {
        if (value.id === findId) {
          lResult = value[key];
        }
      })
      return lResult;
    },
    // 查看述职报告
    szbgOpen(e, id, hdName) {
      e.stopPropagation()
      this.hdglSzAddOrUpdateVisibale = true
      this.$nextTick(() => {
        this.$refs.hdglSzAddOrUpdate.init(id, hdName)
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchList(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      }, this.dataForm)).then(response => {
        this.dataList = response.data.data.records
        this.totalPage = response.data.data.total
        if (this.index === '') {
          this.activeName = '';
        } else {
          this.selinformation(this.index);
        }
      })
      this.dataListLoading = false
    },
    getDataList_new() {
      this.dataListLoading = true
      fetchList_new(Object.assign({
        current: this.pageIndex,
        size: this.pageSize
      }, this.dataForm_35)).then(response => {
        this.dataList_35 = response.data.data.records
        this.totalPage_35 = response.data.data.total
        if (this.index_35 === '') {
          this.activeName_35 = '';
        } else {
          this.selinformation_new(this.index_35);
        }
      })
      this.dataListLoading = false
    },
    //主级修改增加返回
    zjOperation() {
      this.index_35 = '';
      this.getDataList_new();
    },
    // 删除
    deleteHandlehdgl(e, id) {
      var _this = this;
      e.stopPropagation()

      selhdidList(id).then(response => {
        if (response.data.data.length > 0) {
          _this.$notify.closeAll();
          _this.$notify.error('存在下一级无法删除');
        } else {
          _this.$confirm('是否确认删除', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            return delObj(id)
          }).then(data => {
            this.$notify.closeAll();
            _this.$notify.success('删除成功');
            _this.getDataList();
          })
        }
      })
    },
    deleteActivity(e, id) {
      var _this = this;
      e.stopPropagation()

      selhdidList_new(id).then(response => {
        if (response.data.data.length > 0) {
          _this.$notify.closeAll();
          _this.$notify.error('存在下一级无法删除');
        } else {
          _this.$confirm('是否确认删除', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(function () {
            return delObj_new(id)
          }).then(data => {
            this.$notify.closeAll();
            _this.$notify.success('删除成功');
            _this.getDataList_new();
          })
        }
      })
    },
    //取得会议管理List索引
    getHdglbListIndex(pid) {
      let lIndex = 0;
      for (let i = 0; i < this.dataList.length; i++) {
        if (this.dataList[i].id === pid) {
          lIndex = i;
          break;
        }
      }
      return lIndex;
    },
    //删除测评票
    deleteHandleCpp(id) {
      var _this = this;
      this.$confirm('是否确认删除，删除后述职报告也将删除。', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObjCpp(id);
      }).then(data => {
        _this.$notify.closeAll();
        _this.$notify.success('删除成功');
        _this.getDataList();
      });
    },
        //删除测评票
    deleteActSheet(id) {
      var _this = this;
      this.$confirm('是否确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObjCpp_new(id);
      }).then(data => {
        _this.$notify.closeAll();
        _this.$notify.success('删除成功');
        _this.getDataList_new();
      });
    },
    //序号获取
    selxh(xh) {
      return (this.pageIndex - 1) * this.pageSize + (xh + 1);
    },
    // 阻止事件传播
    stopProp: function (e) {
      e.stopPropagation()
    },
    //动态获取树结构
    selinformation(index) {
      this.index = index;
    
      selhdidList(this.dataList[index].id).then(response => {
        this.$set(this.dataList[index], 'sjlist', response.data.data);
      })
    },
    selinformation_new(index) {
      this.index = index;
    
      selhdidList_new(this.dataList_35[index].id).then(response => {
        this.$set(this.dataList_35[index], 'sjlist', response.data.data);
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle_new(val) {
      this.pageSize_35 = val
      this.pageIndex_35 = 1
      this.getDataList_new()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    currentChangeHandle_new(val) {
      this.pageIndex_35 = val
      this.getDataList_new()
    },
    //下级增加测评票最终逻辑
    addOrUpdateHanderCpp(event, parentR, id, index) {
      event.stopPropagation();

      this.hdglCppAddOrUpdateVisibale = true;
      this.$nextTick(() => {
        this.$refs.hdglCppAddOrUpdate.init(id, parentR);
      });
    },
      //下级增加测评票最终逻辑
    addOrUpdateActSheets(event, parentR, id, index) {
      event.stopPropagation();

      this.hdglCppAddOrUpdateVisibale_new = true;
      this.$nextTick(() => {
        this.$refs.activitySheet.init(id, parentR);
      });
    },
    //测评票添加和修改，一级新增
    hdglAddOrUpdateHandle(event, id) {
      event.stopPropagation(); //防止事件传播
      this.hdglAddOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.hdglAddOrUpdate.init(id)
      })
    },
    //表单3-5 活动
    addOrUpdateActiviy(event, id) {
      event.stopPropagation(); //防止事件传播
     //console.log('new_add');
      this.activityVisible = true
      this.$nextTick(() => {
        this.$refs.newAcitivy.init(id)
      })
    },
    //测评票预览
    // reading(r) {
    //   this.cepingpiaoYulanVisible = true;
    //   this.$nextTick(() => {
    //     this.$refs.cepingpiaoYulan.initXp(r)
    //   });
    // },
    reading(r) {
     //console.log(r);
     console.log("r.sheetType:"+r.sheetType)
      if(r.sheetType == '3'){
        this.cepingpiaoYulanVisible3 = true;
        this.$nextTick(() => {
          this.$refs.cepingpiaoYulan3.initXp(r)
        });
      }else if(r.sheetType == '4'){
        this.cepingpiaoYulanVisible4 = true;
        this.$nextTick(() => {
          this.$refs.cepingpiaoYulan4.initXp(r)
        });
      }else if(r.sheetType == '5'){
        this.cepingpiaoYulanVisible5 = true;
        this.$nextTick(() => {
          this.$refs.cepingpiaoYulan5.initXp(r)
        });
      }},
    //会议票概况
    hdpgkOpen(e, PidDate) {
      e.stopPropagation()
      this.cepingphzVisible = true
      this.$nextTick(() => {
        this.$refs.cepingPhz.init(PidDate, null)
      })
    },
    //测评票概况
    cppgkOpen(e, PidDate, ChildDate) {
      e.stopPropagation();
      this.cepingphzVisible = true
      this.$nextTick(() => {
        this.$refs.cepingPhz.init(PidDate, ChildDate)
      })
    },

    //测评票汇总
    cpphzOpen(e, hd, cpp) {
      e.stopPropagation();
      //console.log(hd.id);
      //console.log(cpp.cppId);
      this.$refs.cepinghdcphzDialog.init(hd.id, cpp.cppId)
    },
    //测评票汇总
    hdhzOpen(e, hd) {
      e.stopPropagation();
      //console.log(hd.id);
      //console.log(cpp.cppId);
      this.$refs.cepinghdzhhzDialog.init(hd.id)
    },
    //状态
    Zt(value) {
      return {
        red: value === "未审核",
        blue: value === "审核中",
        green: value === "已审核",
      };
    },
    ConvertShZt(value) {
      var result = ""
      if(value === "0") result = "未审核"
      if(value === "1") result = "审核中"
      if(value === "2") result = "已审核"
      return result

    },
    //会议状态
    hdZt(value) {
      return {
        red: value === "未开始" ||value === "0",
        blue: value === "进行中" ||value === "1",
        green: value === "已结束" ||value === "2",
        yinCang: value === "已到期" ||value === "3",
      };
    },
    //会议状态
    determine(hdZt, hdJssj) {
      if(hdZt === "0") hdZt = "未开始"
      if(hdZt === "1") hdZt = "进行中" 
      if(hdZt === "2") hdZt = "已结束"
      if(hdZt === "3") hdZt = "已到期"    
      if (new Date() > new Date(hdJssj)) {
        if (hdZt != "已结束") {
          return "已到期";
        } else {
          return hdZt;
        }
      }else{
        return hdZt;
      }
    },
    convertZt(cppfs){
       var result = "";
       if(cppfs === "1") result = "单票"
       if(cppfs === "2") result = "部门"
       if(cppfs === "3") result = "个人"
       if(cppfs === "4") result = "部门与个人"
       return result
    }
  }
}
</script>
